<?php
function license_get_list(){
global $_CONFIG;
	$result = mysql_query("
	SELECT *
	FROM " .$_CONFIG['table_permissions'] ."
	");
	$data = array();
	while($tmp = mysql_fetch_assoc($result)){
		array_push($data, $tmp);
	};
	
	return $data;
}

function license_change($userid, $perms){
global $_CONFIG;
	mysql_query("
	UPDATE ". $_CONFIG['table_users'] ."
	SET permissions='".$perms."' WHERE id='".$userid."'
	");
}

function license_new_id(){
global $_CONFIG;
	$result = mysql_query("
	SELECT (id*2) as next_id
	FROM " .$_CONFIG['table_permissions'] ."
	ORDER BY id DESC
	LIMIT 0,1
	");
	if(mysql_num_rows($result) != 0){
		return mysql_result($result, 0, "next_id");
	}else{
		return 1;
	}
}

function license_add($name, $desc){
global $_CONFIG;
	mysql_query("
	INSERT INTO " .$_CONFIG['table_permissions'] ."
	VALUES ('".license_new_id()."','".$name."','".$desc."')
	");
}

function license_del($named){
global $_CONFIG;
$table=$_CONFIG['table_permissions'];
	//mysql_query(" delete * from " .$_CONFIG['table_permissions'] ." where name='" .$named."'");
	echo $table,$named;
	$query1=mysql_query("DELETE FROM ".$table." WHERE name='".$named."'")
				OR DIE("Select Error:".mysql_error());
	/*$action = mysql_query($query1)
				OR DIE("Select Error:".mysql_error());*/
	
}

function license_user_get_perms($id){
global $_CONFIG;
	return intval(mysql_result(mysql_query("
	SELECT permissions
	FROM ". $_CONFIG['table_users'] ."
	WHERE id = '".$id."'
	"), 0 ,'permissions'));
}

function license_has($user, $perm){
global $_CONFIG;
	$perms = license_user_get_perms(user_get_id($user));
	$perm1 = mysql_result(mysql_query("
	SELECT id
	FROM " .$_CONFIG['table_permissions'] ."
	WHERE name = '".$perm."'
	"), 0 ,'id');
	return intval($perms) & intval($perm1);
}


function license_get($user){
global $_CONFIG;
	$perms = license_user_get_perms(user_get_id($user));
	$perm_list = array();
	foreach(license_get_list() as $perm){
		if($perms & intval($perm['id'])){
			$perm_list[] = $perm;
		}
	}
	return $perm_list;
}
?>